﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class AB55 : DocumentBase
    {
        #region-------------Properties-----------------------
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        AB55Data m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataSet setUI1 = new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        AB55Report m_rpt;
        DataTable tableUI;
        public DataSet setResult;
        private DataRow dwUI;
        string FACTORY, STAFF;
        #endregion
        public AB55()
        {
            InitializeComponent();
            this.Name = "询货期单";
            this.Title = "询货期单";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            dsParamaters = (DataSet)dicParamater["dsParamaters"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new AB55Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("KRS", typeof(string));
            tableUI.Columns.Add("LFD", typeof(string));
        }
        private void InitDataBinding()
        {
            try
            {

                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT TRIM(E1.LSNAME1) WFACTORY,TRIM(E1.LSAPNA1) AS STAFF,E.ABPANRKR,E.ABPALFDNR, "
                    + " E.ABPAUFKRS||'-'||TO_NUMBER(E.ABPAUFLFD)||'-'||TO_NUMBER(E.ABPAUFPOS) AS WORDERNO, "
                    + " TRIM(E.ABPART)||'-'||TRIM(E.ABPFARBE)||'-'||E.ABPVAR AS ARTICLE, "
                    + " A.ABPAUFKRS||'-'||TO_NUMBER(A.ABPAUFLFD)||'-'||TO_NUMBER(A.ABPAUFPOS) AS SORDERNO, "
                    + " TRIM(B.LSNAME1) AS SFACTORY,D.ABASANZSTK*D.ABASSTKLG*0.001 AS SMTR, "
                    + " D.ABASANZSTK AS SPCNUM,D.ABASSTKLG*0.001 AS SQTY,A.ABPENDDAT,G.WAKEAFAKTR*0.001 AS RATE,ARWERT*0.001 AS PRICE "
                    + " FROM ABPOS A,(SELECT DISTINCT ABFMANDANT,ABFAUFKRS,ABFAUFLFD,ABFAUFPOS,ABFLIEFNR,C.LSNAME1 "
                    + " FROM ABFOLGE B,LIEFDAT C WHERE B.ABFMANDANT = C.LSMANDANT AND B.ABFLIEFNR = C.LSLIEFNR  "
                    + " AND TRIM(ABFLIEFNR) IS NOT NULL) B, "
                    + " ABAUFM D,ABPOS E,(SELECT DISTINCT ABFMANDANT,ABFAUFKRS,ABFAUFLFD,ABFAUFPOS, "
                    + " ABFLIEFNR,LSNAME1,LSAPNA1 "
                    + " FROM ABFOLGE E1,LIEFDAT E2 WHERE E1.ABFMANDANT = E2.LSMANDANT AND E1.ABFLIEFNR = E2.LSLIEFNR "
                    + "  AND TRIM(ABFLIEFNR) IS NOT NULL) E1,ABPR F,WASTKE G "
                    + " WHERE A.ABPMANDANT = B.ABFMANDANT AND A.ABPAUFKRS = B.ABFAUFKRS "
                    + " AND A.ABPAUFLFD = B.ABFAUFLFD AND A.ABPAUFPOS = B.ABFAUFPOS "
                    + " AND A.ABPMANDANT = D.ABAMANDANT AND A.ABPAUFKRS = D.ABAAUFKRS "
                    + " AND A.ABPAUFLFD = D.ABAAUFLFD AND A.ABPAUFPOS = D.ABAAUFPOS "
                    + " AND D.ABAAPOS >'0001' AND D.ABAMANDANT = E.ABPMANDANT "
                    + " AND D.ABAVKAUFKRS = E.ABPAUFKRS AND D.ABAVKAUFLFD = E.ABPAUFLFD "
                    + " AND D.ABAVKAUFPOS = E.ABPAUFPOS "
                    + " AND E.ABPMANDANT = E1.ABFMANDANT AND E.ABPAUFKRS = E1.ABFAUFKRS "
                    + " AND E.ABPAUFLFD = E1.ABFAUFLFD AND E.ABPAUFPOS = E1.ABFAUFPOS "
                    + " AND E.ABPMANDANT = F.ARMANDANT AND E.ABPAUFKRS = F.ARAUFKRS "
                    + " AND E.ABPAUFLFD = F.ARAUFLFD AND E.ABPAUFPOS = F.ARAUFPOS "
                    + " AND F.ARAFOLGE ='000' AND E.ABPMANDANT = G.WAKMANDANT AND E.ABPART = G.WAKART "
                    + " AND TRIM(E.ABPVAR) = G.WAKVAR AND E.ABPFARBE = G.WAKWCOL AND G.WAKPOS = '01' "
                    + " AND A.ABPAUFART = 'VW' AND A.ABPGESSTA <> 'SP' "
                    + " AND E.ABPAUFART = 'DI' AND E.ABPGESSTA = 'SP' "
                 ;


                dsResult = m_Data.Connect2Proxy(orlReadCommand);

                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "ABPOS";

                    byte[] b;
                    string value;
                    string ColumnName = "";
                    foreach (DataRow dw in dsResult.Tables["ABPOS"].Rows)
                    {
                        ColumnName = "WFACTORY";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "STAFF";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;
                        ColumnName = "SFACTORY";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;

                    }
                }
                setUI.Merge(dsResult);


                this.DataContext = setUI;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;


        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {


            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;

        }

        private void MainDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (e.AddedItems.Count == 0)
                    return;

                DataRowView drv = e.AddedItems[0] as DataRowView;
                dwABPOS = drv.Row;
                this.txtABPAUFKRS.Text = dwABPOS["ABPANRKR"].ToString();
                this.txtABPAUFLFD.Text = dwABPOS["ABPALFDNR"].ToString();
                FACTORY = dwABPOS["WFACTORY"].ToString();
                STAFF = dwABPOS["STAFF"].ToString();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return;
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {

            string KRS = txtABPAUFKRS.Text.Trim();
            string LFD = txtABPAUFLFD.Text.Trim();
            setUI.Clear();
            dicParamater["KRS"] = KRS;
            dicParamater["LFD"] = LFD;
            dicParamater["FACTORY"] = FACTORY;
            dicParamater["STAFF"] = STAFF;
            string PrintType = "JustExcel";
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                m_DataX = new AB55Data(dicParamater);
                m_DataX.exAgent._app.Visible = false;
                m_DataX.OracleDB = this.OracleDB;
                m_DataX.proxy = this.proxy;
                m_rpt = new AB55Report(dicParamater);
                m_rpt.dsSource = setUI1;
                m_rpt.PrintType = PrintType;
                m_rpt.exAgent = m_DataX.exAgent;
                m_rpt.WriteExcelReport("EXCEL");
            }
        }
        private void btnGetData_Click(object sender, RoutedEventArgs e)
        {
            using (new LongOperation(this, "数据查询中"))
            {
                InitDataBinding();
            }
        }

        private void btnQuery_Click(object sender, RoutedEventArgs e)
        {

            string KRS = txtABPAUFKRS.Text;
            string LFD = txtABPAUFLFD.Text.Trim();

            setUI1.Tables.Clear();
            setUI1.AcceptChanges();
            dwUI = tableUI.NewRow();
            dwUI["KRS"] = KRS;
            dwUI["LFD"] = LFD;
            using (new LongOperation(this, "读取符合条件的记录"))
            {
                setResult = m_Data.BuildABPOS(dwUI);
                setUI1.Merge(setResult);
                this.DataContext = setUI1;

            }
        }


    }
}
